<?php

include_once "../../config/connection.php";
	
class Database{

		var $hasil;
        var $connect;
        var $query;

	public function __construct(){
	   
		$this->connect = new Connection();
        
	}
	
	
	public function table_Exists($table){
	   
       $this->connect->connect();
       
		$query = mysql_query("SHOW TABLES FROM ".$this->connect->db." LIKE '".$table."'");
		
        if($query){
            $num=mysql_num_rows($query);
            
            if($num==1){
                //echo "table ada jumlahe ";
                //echo $num."</br>";
                return true;
			
            }else{
                echo "table tidak ada";
                return false;
            }
            
        }else{
            echo "table tidak ada";
            return false;            
        }
        
        $this->connect->disconnect();
		
	} 
	
	//values array
	public function add($table, $value, $row=null){
	   
       $this->connect->connect();
	   
		if($this->table_Exists($table))
        {
            $query = 'INSERT INTO '.$table;
            //menambahkan parameter kolom tabel
            if($row != null)
            {
                $query .= ' ('.$row.')';
            }

            //merubah nilai string di array dengan menambahkan karakter ""
            for($i = 0; $i < count($value); $i++)
            {
                if(is_string($value[$i]))
                    $value[$i] = '"'.$value[$i].'"';
            }
            //menggabung array dengan pemisah tanda koma
            $value = implode(',',$value);
            $query .= ' VALUES ('.$value.')';
            print_r($query);

            $result = mysql_query($query);

            if($result)
            {
                //echo $query." berhasil insert";
                return true;
            }
            else
            {
                return false;
            }
        }
        
        $this->connect->disconnect();
		
	}
	
	public function update($table, $row, $where){
	   
       $this->connect->connect();
       
		if($this->table_Exists($table)){
			
			
			$query = "UPDATE ".$table." SET ";
			
			
			$key= array_keys($row);
			
			for($i=0; $i<count($key); $i++){
				if(is_string($row[$key[$i]]))
                {
                    $query .= $key[$i].'="'.$row[$key[$i]].'"';
                }
                else
                {
                    $query .= $key[$i].'='.$row[$key[$i]];
                }
				
				if($i != count($row)-1){
					$query .= ",";
				}
			}
			//$query .=")";
            
			$query .= " WHERE ".$where;
			
            print_r($query);
			$result = mysql_query($query);
			if($result){
				return true;
			}else{
				return false;
			}
			
			
		}else{
			return false;
		}
        
        $this->connect->disconnect();
        
	}
	
	public function delete($table, $where=null){
	   
       $this->connect->connect();
       
		if($this->table_Exists($table)){
			if(!is_null($where)){
				$query = "DELETE FROM ".$table." WHERE ".$where;
			}else{
				$query = "DELETE FROM ".$table;
			}            
			print_r($query);
			$result = mysql_query($query);
			
			if($result){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
        
        $this->connect->disconnect();
        
	}
	
	public function show_Data($table, $row='*', $posisi=null, $batas=null, $where=null, $order=null){
	   
       $this->connect->connect();
       
	   //$this->connect();
		$query = "SELECT ".$row." FROM ".$table."";
		
		if(!is_null($where)){
			$query .= " WHERE ".$where."";
		}
		
		if(!is_null($order)){
			$query .= " ORDER BY ".$order."";
		}
        
        if(!is_null($posisi)){
            
            $query .= " LIMIT ".$posisi;
        }
        
        if(!is_null($batas)){
            
            $query .= ",".$batas;
        }
        
        //print_r($query);
        $this->query = $query;
		
		if($this->table_Exists($table)){
			
			$result = mysql_query($query);
            
			if($result){
				$numRow = mysql_num_rows($result);
				//$numCol = mysql_num_fields($result);
				
				for($i=0; $i<$numRow;$i++){
					$r = mysql_fetch_array($result);
                    
					$key = array_keys($r);
					
					for($j=0; $j<count($key);$j++){
					   if(!is_int($key[$j]))
						$this->hasil[$i][$key[$j]] = $r[$key[$j]];
					}
				}
				return true;
			}else{
				return false;
			}	
		}else{
			return false;
		}
        
        //print_r($this->hasil);
        $this->connect->disconnect();
	
	}
    
    public function get_hasil(){
        return $this->hasil;
    }
    
    public function select_table($query){
        $this->connect->connect();
        $hasil = array();
                
        
			//print_r($query);
			$result = mysql_query($query);
            
			if($result){
				$numRow = mysql_num_rows($result);				
				
				for($i=0; $i<$numRow;$i++){
					$r = mysql_fetch_array($result);
                    
					$key = array_keys($r);
					
					for($j=0; $j<count($key);$j++){
					   if(!is_int($key[$j]))
						$hasil[$i][$key[$j]] = $r[$key[$j]];
					}
				}
				//return true;
			}else{
				return false;
			}	
		
        $this->connect->disconnect();
        
        return $hasil;
    }
	
}

//$db = new Database();

//$db->table_Exists("user");



